package tech.aistar.config;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import tech.aistar.anno.CurrUser;
import tech.aistar.pojo.User;

/**
 * 本来用来演示: 专门用来处理@CurrUser
 */
public class HandlerArgsConfig implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        //必须加的是@CurrUser,并且这个注解必须修饰的User
        return parameter.getParameterAnnotation(CurrUser.class)!=null && parameter.getParameterType()== User.class;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
        //将这个request作用域中的数据,绑定到@CurrUser这个注解修饰的那个对象上去
        return request.getAttribute("currUser");
    }
}
